fix(wecom): fallback to message API when kf returns 40096#7012
fix(wecom): fallback to message API when kf returns 40096#7012Soulter merged 2 commits intoAstrBotDevs:masterfrom
Conversation
When sending WeCom messages via kf/send_msg, if the API returns error 40096 (invalid external userid), fall back to the regular message/send API. This handles internal employees who don't have external userids. Fixes the issue where internal WeCom users (e.g. WangCong) would cause kf API to fail with 'invalid external userid' error.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of WeCom message delivery by introducing a critical error handling mechanism. It ensures that messages are successfully sent to all recipients, including internal employees who might lack external user IDs, by automatically switching from the customer service API to the standard message API upon encountering a specific error code. This improvement prevents message delivery failures and maintains consistent communication within the WeCom platform. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider extracting the magic error code
40096into a named constant (e.g.ERR_INVALID_EXTERNAL_USERID) near the WeCom integration code so the meaning is documented and easier to reuse or change. - In the warning log on fallback, it may be helpful to include
e.errmsgor the exception string as well as the hardcoded code, so unexpected variations of the error are easier to diagnose.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the magic error code `40096` into a named constant (e.g. `ERR_INVALID_EXTERNAL_USERID`) near the WeCom integration code so the meaning is documented and easier to reuse or change.
- In the warning log on fallback, it may be helpful to include `e.errmsg` or the exception string as well as the hardcoded code, so unexpected variations of the error are easier to diagnose.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request introduces error handling for sending text messages via the WeCom customer service API. It specifically catches WeChatClientException with errcode 40096 (invalid external user ID) and falls back to the regular WeCom message API, logging a warning. The review feedback suggests extending this fallback logic to other message types (Image, Record, File, Video) that also use kf_message_api.send_* methods, as they could potentially encounter the same 40096 error.
| try: | ||
| kf_message_api.send_text(user_id, self.get_self_id(), chunk) | ||
| except WeChatClientException as e: | ||
| if getattr(e, 'errcode', None) == 40096: | ||
| # 40096: invalid external userid, fallback to regular message API | ||
| logger.warning(f"kf API error 40096 for user {user_id}, falling back to regular message API") | ||
| self.client.message.send_text(self.get_self_id(), user_id, chunk) | ||
| else: | ||
| raise |
There was a problem hiding this comment.
The fallback logic for errcode 40096 is currently implemented only for Plain (text) messages. However, other message types such as Image, Record, File, and Video also use kf_message_api.send_* methods, which could potentially return the same 40096 error. To ensure a comprehensive solution, consider applying similar try...except blocks with fallback logic to these other message types as well.
Summary
When kf/send_msg returns error 40096 (invalid external userid), fall back to regular message/send API. This handles internal WeCom employees who don't have external userids.
Changes
In
astrbot/core/platform/sources/wecom/wecom_event.py:WeChatClientExceptionimport fromwechatpy.exceptionskf_message_api.send_text()in try/except blockerrcode == 40096: log warning and callself.client.message.send_text()insteadTesting
Production tested on zuju.hortorcreative.com - log shows:
kf API error 40096 for user WangCong, falling back to regular message APISummary by Sourcery
Bug Fixes: